import Vue from 'vue'
import Router from 'vue-router'
import Login from "@/components/Login";
import Register from "@/components/Register";
import HelloWorld from "@/components/HelloWorld";
import Publish from "@/components/Publish";
import Mine from "@/components/Mine";
import Article from "@/components/Article";

Vue.use(Router)

const router = new Router({
    routes: [
        {
            path: '/',
            meta: {
              requireAuth: true
            },
            component: HelloWorld },
        { path: '/login', component: Login },
        { path: '/register', component: Register },
        { path: '/publish', component: Publish },
        { path: '/mine', component: Mine },
        { path: '/article/:id', component: Article }
    ]
})

// 拦截守卫，进行登录拦截，要求必须登录
router.beforeEach((to, from, next) => {
    if (to.path === '/login' || to.path === '/register')
        return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if(!tokenStr)
        return next('/login')
    next()
})

// 挂载

export default router